package com.module.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.module.framework.constants.SystemConstants;
import com.module.framework.domain.Category;
import com.module.framework.domain.Product;
import com.module.framework.domain.Role;
import com.module.framework.mapper.CategoryMapper;
import com.module.framework.service.CategoryService;
import com.module.framework.service.ProductService;
import com.module.framework.utils.BeanCopyUtils;
import com.module.framework.vo.CategoryVO;
import com.module.framework.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author JiHuiQin
 * @Date 2022/2/14 11:50
 * @Version 1.0
 */

@Service
public class CategroyServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * public List<CategoryVO> getCategoryList() {
     * LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
     * productLambdaQueryWrapper.eq(Product::getStatus, SystemConstants.ZERO_FLAG);  //已经发布的
     * List<Product> productList = productService.list(productLambdaQueryWrapper);
     * //获取产品分类id，并且去重
     * Set<Integer> categoryIds = productList.stream().map(product -> product.getCateId()).collect(Collectors.toSet());
     * //查询分类信息
     * List<Category> categories = listByIds(categoryIds);
     * List<Category> categoryList = categories.stream().filter(category -> SystemConstants.S_ZERO_FLAG.equals(category.getStatus())).collect(Collectors.toList());
     * //封装vo
     * List<CategoryVO> categoryVOS = BeanCopyUtils.copyBeanList(categoryList, CategoryVO.class);
     * return categoryVOS;
     * }
     */

    @Override
    public PageVO getCategoryList(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(Objects.nonNull(name), Category::getName, name);
        PageVO pageVO = null;
        if (pageNum == null && pageSize == null) {
            List<Category> lists = list(queryWrapper);
            pageVO = new PageVO(lists, lists.size());
        } else {
            //分页查询
            pageSize = pageSize == null ? SystemConstants.PAGE_SIZE : pageSize;
            Page<Category> page = new Page<>(pageNum, pageSize);
            page(page, queryWrapper);
            List<Category> records = page.getRecords();
            pageVO = new PageVO(records, (int) page.getTotal());
        }
        return pageVO;
    }

    @Override
    public void addCategory(Category category) {
        categoryMapper.insert(category);
    }

    @Override
    public void updateCategory(Category category) {
        categoryMapper.updateById(category);
    }

    @Override
    public void deleteCategory(Integer id) {
        categoryMapper.deleteById(id);
    }
}
